*************************************************************************
*																		*
*					Campaign Panel Book Chapter 6 - Party Vote Models	*
*																		*
*************************************************************************	

use "${path}\Reproducible Do-Files\Chapter 6\Data\longformat09", clear

xtset lfdn welle


*---------------------------------
* Locals for IVs
*-----------------------------------

/* Independent of the party we are analysing vote intention for, the independent
variables are always the same ones (apart from solution competence). There are 
different blocks of variables: attitudes, communication and time. These independent
variables are concentrated in locals to make the code easier to understand 
as well as shorter */


local kommunikation awl tvduell1 tvduell4 tvduell_su1 tvduell_su4 tvduell_md1 tvduell_md4 pk_gesamtin? gespr? 
display "`kommunikation'"

local time w2 w3 w4 w5 w6 w7
display "`time'"

foreach partei of numlist 1 4/7 {
local attitudes`partei' koasge koagk koarg koarrg regzufr kand1 kand4  kand5 kand6 kand7 komp`partei'
display " `attitudes`partei''"
}

* No missing on any variable
egen rowmiss = rowmiss(parta1 `kommunikation' `attitudes1')

drop if rowmiss~=0  // no problem with W2-starters because of dropping on the level
					// of observation, W2-starter individuals are generally 
					// included, just not in wave 1
bysort lfdn: egen total = total(include)
fre total
						
*Change working directory

cd "${path}\Reproducible Do-Files\Chapter 6"

*----------------------------------------------------
* Separate according to party identification
*----------------------------------------------------

/* 
model 1 = only attitudes
model 2 = only communication
model 3 = attitudes + communication
*/

estimates clear

foreach num of numlist 1 4/7  { 

xtlogit parta`num' `attitudes`num'' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model1_`num'						//store regression results
/*gen model1_`num' = e(sample)						//store sample information in variable
estpost sum `attitudes`num'' if e(sample)==1		//store means of independent variables

estimates store model1_`num'_uvs 	*/

xtlogit parta`num' `kommunikation' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model2_`num'
/*gen model2_`num' = e(sample)
estpost sum `kommunikation' if e(sample)==1
estimates store model2_`num'_uvs */

xtlogit parta`num' `kommunikation' `attitudes`num'' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model3_`num'
gen model3_`num' = e(sample)				//store sample information in variable
estpost sum `kommunikation' `attitudes`num'' if e(sample)==1		//store means of independent variables
estimates store model3_`num'_uvs 

	foreach num2 of numlist 1 4/7 808 {
				
	xtlogit parta`num' `attitudes`num'' `time' ///
		if neverno==1 & rowmiss==0 & pidfull==`num2', fe or iterate(10)
	estimates store model1_`num'_`num2'
	/* gen model1_`num'_`num2' = e(sample)
	estpost sum `attitudes`num'' if e(sample)==1
	estimates store model1_`num'_`num2'_uvs */

	xtlogit parta`num' `kommunikation' `time' ///
		if neverno==1 & rowmiss==0 & pidfull==`num2', fe or iterate(10) 
	estimates store model2_`num'_`num2'
	/* gen model2_`num'_`num2' = e(sample)
	estpost sum `kommunikation' if e(sample)==1
	estimates store model2_`num'_`num2'_uvs */
	
	xtlogit parta`num' `kommunikation' `attitudes`num'' `time' ///
		if neverno==1 & rowmiss==0 & pidfull==`num2', fe or iterate(10) 
	estimates store model3_`num'_`num2'
	gen model3_`num'_`num2' = e(sample)
	estpost sum `kommunikation' `attitudes`num''  if e(sample)==1
	estimates store model3_`num'_`num2'_uvs 

	}
}


* Tabulate regression results
esttab model1_1 model1_1_1 model1_1_4 model1_1_5 model1_1_6 model1_1_7 model1_1_808 ///
	using tables_Model1_CDU_A09.rtf, order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model1_4 model1_4_1 model1_4_4 model1_4_5 model1_4_6 model1_4_7 model1_4_808 ///
	using tables_Model1_SPD_A09.rtf, order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model1_5 model1_5_1 model1_5_4 model1_5_5 model1_5_6 model1_5_7 model1_5_808 ///
	using tables_Model1_FDP_A09.rtf, order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model1_6 model1_6_1 model1_6_4 model1_6_5 model1_6_6 model1_6_7 model1_6_808 ///
	using tables_Model1_GRU_A09.rtf, order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model1_7 model1_7_1 model1_7_4 model1_7_5 model1_7_6 model1_7_7 model1_7_808 using tables_Model1_LIN_A09.rtf, ///
	order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 

esttab model2_1 model2_1_1 model2_1_4 model2_1_5 model2_1_6 model2_1_7 model2_1_808 using tables_Model2_CDU_A09.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model2_4 model2_4_1 model2_4_4 model2_4_5 model2_4_6 model2_4_7 model2_4_808 using tables_Model2_SPD_A09.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model2_5 model2_5_1 model2_5_4 model2_5_5 model2_5_6 model2_5_7 model2_5_808 using tables_Model2_FDP_A09.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model2_6 model2_6_1 model2_6_4 model2_6_5 model2_6_6 model2_6_7 model2_6_808 using tables_Model2_GRU_A09.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model2_7 model2_7_1 model2_7_4 model2_7_5 model2_7_6 model2_7_7 model2_7_808 using tables_Model2_LIN_A09.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
	
	
esttab model3_1 model3_1_1 model3_1_4 model3_1_5 model3_1_6 model3_1_7 model3_1_808 using tables_model3_CDU_A09.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model3_4 model3_4_1 model3_4_4 model3_4_5 model3_4_6 model3_4_7 model3_4_808 using tables_model3_SPD_A09.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model3_5 model3_5_1 model3_5_4 model3_5_5 model3_5_6 model3_5_7 model3_5_808 using tables_model3_FDP_A09.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model3_6 model3_6_1 model3_6_4 model3_6_5 model3_6_6 model3_6_7 model3_6_808 using tables_model3_GRU_A09.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model3_7 model3_7_1 model3_7_4 model3_7_5 model3_7_6 model3_7_7 model3_7_808 using tables_model3_LIN_A09.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 	

* How many Crystalizers, Waverers and Party Changers do the individual models contain?
foreach model of varlist model3* {
estpost tab laz if `model'==1 & welle==1, m
estimates store laz`model'
}

	
esttab lazmodel3_1* using "descriptives crystallizer in models09.rtf", ///
	cells("b pct(fmt(0))") compress replace  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2009: Anteil Kristallisierer, Waverer und Party Changer in CDU-Wahl-Modellen zu Welle 1")
esttab lazmodel3_4* using "descriptives crystallizer in models09.rtf", ///
	cells("b pct(fmt(0))") compress append  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2009: Anteil Kristallisierer, Waverer und Party Changer in SPD-Wahl-Modellen zu Welle 1")
esttab lazmodel3_5* using "descriptives crystallizer in models09.rtf", ///
	cells("b pct(fmt(0))") compress append  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2009: Anteil Kristallisierer, Waverer und Party Changer in FDP-Wahl-Modellen zu Welle 1")
esttab lazmodel3_6* using "descriptives crystallizer in models09.rtf", ///
	cells("b pct(fmt(0))") compress append  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2009: Anteil Kristallisierer, Waverer und Party Changer in Grünen-Wahl-Modellen zu Welle 1")
esttab lazmodel3_7* using "descriptives crystallizer in models09.rtf", ///
	cells("b pct(fmt(0))") compress append  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2009: Anteil Kristallisierer, Waverer und Party Changer in Linke-Wahl-Modellen zu Welle 1")




*-----------------------------------------------------------------------
* Combine individual party identification groups because of case number problems
*-------------------------------------------------------------------------

lab define ff ///
	1 "own party" ///
	2 "friend" ///
	3 "enemy" ///
	4 "Left ID" ///
	5 "No PID"
	
//CDU/CSU as DV
gen piddep1 = .
replace piddep1 = 1 if pidfull1==1 					//self
replace piddep1 = 2 if pidfull1==5				//friend
replace piddep1 = 3 if inlist(pidfull, 4, 6, 7) //foe
replace piddep1 = 5 if pidfull==808


//SPD as DV
gen piddep4 = .
replace piddep4 = 1 if pidfull1==4
replace piddep4 = 2 if pidfull1==6
replace piddep4 = 3 if inlist(pidfull, 1, 5)
replace piddep4 = 4 if pidfull1==7
replace piddep4 = 5 if pidfull==808

//FDP as DV
gen piddep5 = .
replace piddep5 = 1 if pidfull1==5
replace piddep5 = 2 if pidfull1==1
replace piddep5 = 3 if inlist(pidfull, 4, 6, 7)
replace piddep5 = 5 if pidfull==808


//Greens as DV
gen piddep6 = .
replace piddep6 = 1 if pidfull1==6
replace piddep6 = 2 if pidfull1==4
replace piddep6 = 3 if inlist(pidfull, 1, 5)
replace piddep6 = 4 if pidfull1==7
replace piddep6 = 5 if pidfull==808

//Left as DV
gen piddep7 = .
replace piddep7 = 1 if pidfull1==7  				//own
replace piddep7 = 2 if pidfull1==4 | pidfull1==6	//other Lefties
replace piddep7 = 3 if inlist(pidfull, 1, 5)		//righties
replace piddep7 = 5 if pidfull==808

//Labels
lab val piddep? ff

/* If the DV = CDU or FDP or Left, pidep? cannot be equal to four. Therefore
two loops have to be written for the calculation of the loops */

* Calculate models for CDU, FDP and Left vote intention
estimates clear
drop model*


foreach num of numlist 1 5 7  { 

xtlogit parta`num' `attitudes`num'' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model1_`num'						//store regression results

xtlogit parta`num' `kommunikation' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model2_`num'

	foreach num2 of numlist 1 2 3 5 {		
	xtlogit parta`num' `attitudes`num'' `time' ///
		if neverno==1 & rowmiss==0 & piddep`num'==`num2', fe or iterate(10)
	estimates store model1_`num'_`num2'

	xtlogit parta`num' `kommunikation' `time' ///
		if neverno==1 & rowmiss==0 & piddep`num'==`num2', fe or iterate(10) 
	estimates store model2_`num'_`num2'

	}
}


* Calculate models for SPD and Green vote intention
foreach num of numlist 4 6 { 

xtlogit parta`num' `attitudes`num'' `time' if neverno==1, fe or iterate(10) 
estimates store model1_`num'						//store regression results

xtlogit parta`num' `kommunikation' `time' if neverno==1, fe or iterate(10) 
estimates store model2_`num'

	foreach num2 of numlist 1/5 {
				
	xtlogit parta`num' `attitudes`num'' `time' ///
		if neverno==1 & rowmiss==0 & piddep`num'==`num2', fe or iterate(10)
	estimates store model1_`num'_`num2'

	xtlogit parta`num' `kommunikation' `time' ///
		if neverno==1 & rowmiss==0 & piddep`num'==`num2', fe or iterate(10) 
	estimates store model2_`num'_`num2'

	}
}



* Tabulate regression results
esttab model1_1 model1_1_? ///
	using table50.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not replace compress ///
	mtitle("All" "CDU/CSU" "FDP" "Left Parties" "Indep.") nonumbers star(* 0.05)
	
esttab model1_4 model1_4_? ///
	using table51.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not replace compress ///
	mtitle("All" "SPD" "Greens" "CDU/CSU FDP" "The Left" "Indep.") nonumbers star(* 0.05)
	
	
esttab model1_5 model1_5_? ///
	using table52.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not replace compress ///
	mtitle("All" "FDP" "CDU/CSU" "Left Parties" "Indep.") nonumbers star(* 0.05)
	
esttab model1_6 model1_6_1 model1_6_2 model1_6_3 model1_6_5 ///
	using table53.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not replace compress ///
	mtitle("All" "Greens" "SPD" "CDU/CSU FDP" "Indep.") nonumbers star(* 0.05)
	
	
esttab model1_7 model1_7_? ///
	using table54.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not replace compress ///
	mtitle("All" "The Left" "SPD Greens" "CDU/CSU FDP" "Indep.") nonumbers star(* 0.05)
	
esttab model2_1 model2_4 model2_5 model2_6 model2_7 ///
	using table55.rtf, replace compress ///
	sca(N_g) b(2) keep(tvduell*) mtitle("CDU/CSU" "SPD" "FDP" "Greens" "The Left") ///
	nonumbers star(* 0.05) not
	
esttab model2_1 model2_4 model2_5 model2_6 model2_7 ///
	using table56.rtf, replace compress ///
	sca(N_g) b(2) keep(pk* gespr*) mtitle("CDU/CSU" "SPD" "FDP" "Greens" "The Left") ///
	nonumbers star(* 0.05) not
	
esttab model2_1 model2_4 model2_5 model2_6 model2_7 ///
	using table57.rtf, replace compress ///
	sca(N_g) b(2) keep(awl) mtitle("CDU/CSU" "SPD" "FDP" "Greens" "The Left") ///
	nonumbers star(* 0.05) not

estimates clear
